home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / OrganicComboBoxButton.java < prev    next >
Text File  |  1998-06-30  |  7KB  |  242 lines

  1. /*
  2.  * @(#)OrganicComboBoxButton.java    1.6 98/04/10
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20.  
  21. package com.sun.java.swing.plaf.organic;
  22.  
  23. import java.awt.*;
  24. import java.awt.event.*;
  25. import com.sun.java.swing.plaf.basic.*;
  26. import com.sun.java.swing.*;
  27. import com.sun.java.swing.plaf.*;
  28. import com.sun.java.swing.border.*;
  29.  
  30.  
  31. /**
  32.  * JButton subclass to help out OrganicComboBoxUI
  33.  * <p>
  34.  * Warning: serialized objects of this class will not be compatible with
  35.  * future swing releases.  The current serialization support is appropriate
  36.  * for short term storage or RMI between Swing1.0 applications.  It will
  37.  * not be possible to load serialized Swing1.0 objects with future releases
  38.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  39.  * baseline for the serialized form of Swing objects.
  40.  *
  41.  * @see OrganicComboBoxButton
  42.  * @version 1.6 04/10/98
  43.  * @author Tom Santos
  44.  */
  45. public class OrganicComboBoxButton extends JButton {
  46. /*
  47.     protected JComboBox comboBox;
  48.     protected Icon comboIcon;
  49.     protected boolean iconOnly = false;
  50.     protected boolean forceDrawFocus = false;
  51.  
  52.     public final JComboBox getComboBox() { return comboBox; }
  53.     public final void setComboBox( JComboBox cb ) { comboBox = cb; }
  54.  
  55.     public final Icon getComboIcon() { return comboIcon; }
  56.     public final void setComboIcon( Icon i ) { comboIcon = i; }
  57.  
  58.     static OrganicComboBoxButtonBorder comboBoxBorder = new OrganicComboBoxButtonBorder();
  59.  
  60.     OrganicComboBoxButton() {
  61.         super( "" );
  62.     }
  63.  
  64.     public OrganicComboBoxButton( JComboBox cb, Icon i ) {
  65.     super( "" );
  66.  
  67.         comboBox = cb;
  68.     comboIcon = i;
  69.     }
  70.  
  71.     public OrganicComboBoxButton( JComboBox cb, Icon i, boolean onlyIcon ) {
  72.         this( cb, i );
  73.         iconOnly = onlyIcon;
  74.     }
  75.  
  76.     public Insets getInsets() {
  77.         return comboBox != null ? comboBox.getInsets() : new Insets( 0, 0, 0, 0 );
  78.     }
  79.  
  80.     public void paint( Graphics g ) {
  81.         // Paint the button as usual
  82.         super.paint( g );
  83.  
  84.     Dimension size = getSize();
  85.     Insets insets = getInsets();
  86.     size.width -= insets.left + insets.right;
  87.     size.height -= insets.top + insets.bottom;
  88.     int iconLeft = insets.left + size.width;
  89.  
  90.     // Paint the icon
  91.     if ( comboIcon != null ) {
  92.         int iconWidth = comboIcon.getIconWidth();
  93.         final int RIGHT_BUFFER = 8;
  94.  
  95.         if ( iconOnly ) {
  96.             iconLeft = (insets.left + (size.width / 2)) - (iconWidth / 2);
  97.         }
  98.         else {
  99.             iconLeft -= iconWidth + RIGHT_BUFFER;
  100.         }
  101.  
  102.         int iconHeight = comboIcon.getIconHeight();
  103.         int iconTop = (insets.top + (size.height / 2)) - (iconHeight / 2);
  104.  
  105.         comboIcon.paintIcon( this, g, iconLeft, iconTop );
  106.     }
  107.  
  108.     // Let the renderer paint
  109.     if ( ! iconOnly && comboBox != null) {
  110.         OrganicComboBoxUI ui = (OrganicComboBoxUI)comboBox.getUI();
  111.         ListCellRenderer renderer = comboBox.getRenderer();
  112.         Component c;
  113.         ui.validateMenu();
  114.         boolean renderPressed = getModel().isPressed();
  115.         c = renderer.getListCellRendererComponent(ui.getList(),
  116.                               comboBox.getSelectedItem(), -1, renderPressed, false);
  117.         c.setFont(ui.getCurrentValuePane().getFont());
  118.  
  119.             if (model.isArmed() && model.isPressed()) {
  120.             if (isOpaque()) {
  121.             c.setBackground(UIManager.getColor("Button.pressed"));
  122.         }
  123.         } else {
  124.         c.setBackground(comboBox.getBackground());
  125.         }
  126.         c.setForeground(comboBox.getForeground());
  127.  
  128.         ui.getCurrentValuePane().paintComponent( g, c, this, insets.left+4, insets.top+4,
  129.                              (iconLeft - 8) - insets.left,
  130.                              insets.top + size.height - 8 );
  131.     }
  132.  
  133.     if ( (isFocusPainted() && hasFocus()) || forceDrawFocus ) {
  134.         OrganicComboBoxButtonUI buttonUI = (OrganicComboBoxButtonUI)getUI();
  135.         buttonUI.paintFocus( g, insets.left+3, insets.top+3, size.width-7, size.height-7 );
  136.     }
  137.     }
  138.  
  139.     public void forceDrawFocus( boolean b ) {
  140.         forceDrawFocus = b;
  141.     }
  142.  
  143.     public boolean isFocusTraversable() {
  144.         return false;
  145.     }
  146.  
  147.     public void requestFocus() { }
  148.  
  149.     public void updateUI() {
  150.         setUI( new OrganicComboBoxButtonUI( this ) );
  151.     }
  152.  
  153.     class OrganicComboBoxButtonUI extends OrganicButtonUI {
  154.         OrganicComboBoxButtonListener newListener;
  155.  
  156.         public OrganicComboBoxButtonUI( OrganicComboBoxButton button ) {
  157.         newListener = new OrganicComboBoxButtonListener( button );
  158.     }
  159.  
  160.         public void installUI( JComponent c ) {
  161.         super.installUI( c );
  162.         LookAndFeel.uninstallBorder(c);
  163.         c.setBorder( comboBoxBorder );
  164.  
  165.         removeMouseListener( listener );
  166.         removeMouseMotionListener( listener );
  167.         addMouseListener( newListener );
  168.         addMouseMotionListener( (MouseMotionListener)newListener );
  169.     }
  170.  
  171.         public void uninstallUI( JComponent c ) {
  172.         removeMouseListener( newListener );
  173.         removeMouseMotionListener( (MouseMotionListener)newListener );
  174.         super.uninstallUI( c );
  175.     }
  176.     }
  177.  
  178.     class OrganicComboBoxButtonListener extends BasicButtonListener {
  179.         OrganicComboBoxButton button;
  180.         boolean isDraggingProtection = false;
  181.  
  182.         public OrganicComboBoxButtonListener( OrganicComboBoxButton aButton ) {
  183.         super( aButton );
  184.         button = aButton;
  185.     }
  186.  
  187.         public void mousePressed(MouseEvent anEvent) {
  188.         super.mousePressed( anEvent );
  189.  
  190.         OrganicComboBoxUI ui = (OrganicComboBoxUI)button.getComboBox().getUI();
  191.  
  192.         if ( isDraggingProtection ) {
  193.             return;
  194.         }
  195.         else {
  196.             isDraggingProtection = true;
  197.         }
  198.  
  199.         if(!comboBox.isEnabled())
  200.             return;
  201.  
  202.         if(!SwingUtilities.isLeftMouseButton(anEvent))
  203.             return;
  204.  
  205.         if ( ui.popupIsVisible() ) {
  206.             ui.hidePopup();
  207.         }
  208.         else {
  209.             ui.showPopup();
  210.         }
  211.     }
  212.       
  213.         public void mouseReleased(MouseEvent anEvent) {
  214.         isDraggingProtection = false;
  215.         OrganicComboBoxUI ui = (OrganicComboBoxUI)button.getComboBox().getUI();
  216.         if ( !(SwingUtilities.getLocalBounds(button).contains(anEvent.getPoint())) ) {
  217.             ui.doMouseReleased( anEvent );
  218.         }
  219.         super.mouseReleased( anEvent );
  220.     }
  221.  
  222.         public void mouseDragged(MouseEvent anEvent) {
  223.         OrganicComboBoxUI ui = (OrganicComboBoxUI)button.getComboBox().getUI();
  224.         if ( !(SwingUtilities.getLocalBounds(button).contains(anEvent.getPoint())) ) {
  225.             ui.doMouseDragged( anEvent );
  226.         }
  227.     }
  228.     };
  229. */
  230. }
  231.  
  232. //
  233. // This class is here to disable the green "default" border that buttons normally have
  234. //
  235. class OrganicComboBoxButtonBorder extends OrganicButtonBorder {
  236.     public OrganicComboBoxButtonBorder() {
  237.         super();
  238.     }
  239.  
  240.     protected int getDefaultBorderWidth() { return 0; }
  241. }
  242.